package com.example.waarable;

import com.example.waarable.slice.MainAbilitySlice;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.event.intentagent.IntentAgent;
import ohos.event.intentagent.IntentAgentConstant;
import ohos.event.intentagent.IntentAgentHelper;
import ohos.event.intentagent.IntentAgentInfo;
import ohos.event.notification.NotificationHelper;
import ohos.event.notification.NotificationRequest;
import ohos.event.notification.NotificationSlot;
import ohos.media.image.ImageSource;
import ohos.media.image.PixelMap;
import ohos.rpc.RemoteException;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class MainAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setMainRoute(MainAbilitySlice.class.getName());

        NotificationSlot slot = new NotificationSlot("slot_id", "slot_name", NotificationSlot.LEVEL_DEFAULT);
        slot.setDescription("描述文案");
        slot.setLockscreenVisibleness(NotificationRequest.VISIBLENESS_TYPE_PRIVATE);
        try {
            NotificationHelper.addNotificationSlot(slot);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        int notification_id = 1;
        NotificationRequest request = new NotificationRequest(notification_id);
        request.setSlotId(slot.getId());
        String title = "通知标题";
        String text = "通知内容";
        NotificationRequest.NotificationNormalContent content = new NotificationRequest.NotificationNormalContent();
        content.setTitle(title)
                .setText(text);
        NotificationRequest.NotificationContent notificationContent = new NotificationRequest.NotificationContent(content);
        // 设置通知的小图标
        String filePath = String.format(Locale.ROOT, "assets/waarable/resources/base/media/%s", "icon.png");
        InputStream stream = this.getClass().getClassLoader().getResourceAsStream(filePath);
        ImageSource.SourceOptions options = new ImageSource.SourceOptions();

        ImageSource source = ImageSource.create(stream, options);
        ImageSource.DecodingOptions decodingOpts = new ImageSource.DecodingOptions();
        PixelMap pixelMap = source.createPixelmap(decodingOpts);
        request.setLittleIcon(pixelMap);
        // 设置通知的内容
        request.setContent(notificationContent);

        // 指定要启动的ability属性
        Intent oepnIntent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withDeviceId("")
                .withBundleName("com.example.waarable")
                .withAbilityName("com.example.waarable.MainAbility")
                .build();
        oepnIntent.setOperation(operation);
        List<Intent> intentList = new ArrayList<>();
        intentList.add(intent);
        // 指定启动一个有页面的ability
        IntentAgentInfo intenAgentinfo = new IntentAgentInfo(request.getNotificationId(), IntentAgentConstant.OperationType.START_ABILITY, IntentAgentConstant.Flags.UPDATE_PRESENT_FLAG, intentList, null);
        // 获取IntentAgent实例
        IntentAgent intentAgent = IntentAgentHelper.getIntentAgent(this, intenAgentinfo);
        request.setIntentAgent(intentAgent);
        request.setTapDismissed(true);

        try {
            NotificationHelper.publishNotification(request);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }
}
